package com.ruoyi.bus.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.bus.service.NoGenerateServlce;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.Message;
import com.ruoyi.bus.service.IMessageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;

/**
 * 消息Controller
 *
 * @author ruoyi
 * @date 2020-05-01
 */
@Controller
@RequestMapping("/bus/message")
public class MessageController extends BaseController {
    private String prefix = "bus/message";

    @Autowired
    private IMessageService messageService;

    @Autowired
    private NoGenerateServlce noGenerateServlce;

    @RequiresPermissions("bus:message:view")
    @GetMapping()
    public String message() {
        return prefix + "/message";
    }

            /**
         * 查询消息列表
         */
        @RequiresPermissions("bus:message:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(Message message) {
            startPage();
            return getDataTable(messageService.list(new QueryWrapper<>(message)));
        }
    
    /**
     * 导出消息列表
     */
    @RequiresPermissions("bus:message:export")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Message message) {
        List<Message> list = messageService.list(new QueryWrapper<>(message));
        ExcelUtil<Message> util = new ExcelUtil<>(Message.class);
        return util.exportExcel(list, "message");
    }

            /**
         * 新增消息
         */
        @GetMapping("/add")
        public String add() {
            return prefix + "/add";
        }
    
    /**
     * 新增保存消息
     */
    @RequiresPermissions("bus:message:add")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Message message) {
        message.setMessageId(noGenerateServlce.getSeqNo("MS",8));
        return toAjax(messageService.save(message));
    }

    /**
     * 修改消息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Message message = messageService.getById(id);
        mmap.put("message", message);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @RequiresPermissions("bus:message:edit")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Message message) {
        return toAjax(messageService.updateById(message));
    }

            /**
         * 删除消息
         */
        @RequiresPermissions("bus:message:remove")
        @Log(title = "消息", businessType = BusinessType.DELETE)
        @PostMapping( "/remove")
        @ResponseBody
        public AjaxResult remove(String ids) {
            return toAjax(messageService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
        }
        }
